/*
 * Copyright (c) 2025 彭友聪
 * nativePC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2. 
 * You may obtain a copy of Mulan PSL v2 at:
            http://license.coscl.org.cn/MulanPSL2 
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.  
 * See the Mulan PSL v2 for more details.  
 * 
 * Author: 彭友聪 
 * email：2923616405@qq.com 
 * date: 2025/10/26 10:29
 * file: ${FILE_NAME}
 * product: DevEco Studio
 * LICENSE: MulanPSL-2.0
 * */
//
// Created on 2025/10/26.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#ifndef NATIVEPC_TESTGESTUREEXAMPLE_H
#define NATIVEPC_TESTGESTUREEXAMPLE_H
#include "ArkUIBaseNode.h"
#include "ArkUIColumnNode.h"
#include "ArkUITextNode.h"
#include "napi/native_api.h"
#include <memory>
#include <arkui/native_gesture.h>
#include <hilog/log.h>


namespace NativeModule {
    // 创建 Column 节点
    std::shared_ptr<ArkUIBaseNode> testGestureExample() {
    auto column = std::make_shared<ArkUIColumnNode>();
    column->SetBackgroundColor(0xff112233);
    column->SetHeight(200);
    column->SetWidth(200);

    // 判断是否支持创建手势
    auto gestureApi = reinterpret_cast<ArkUI_NativeGestureAPI_1 *>(
        OH_ArkUI_QueryModuleInterfaceByName(ARKUI_NATIVE_GESTURE, "ArkUI_NativeGestureAPI_1"));
    if (gestureApi->createGroupGesture) {
        OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager",
                     "GestureSampleLog, createGroupGesture api exist");
        // 创建手势组，并设置识别模式为顺序识别
        auto groupGesture = gestureApi->createGroupGesture(ArkUI_GroupGestureMode::SEQUENTIAL_GROUP);
        // 获取触发该事件的组件对象
        auto nodeHandle = column->GetHandle();
        if (nodeHandle == nullptr) {
            return nullptr;
        }
        // 创建长按手势
        auto longPressGesture = gestureApi->createLongPressGesture(1, true, 500);
        if (gestureApi->getGestureType) {
            ArkUI_GestureRecognizerType type = gestureApi->getGestureType(longPressGesture);
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager",
                         "GestureSampleLog longPressGesture,ArkUI_GestureRecognizerType%{public}d", type);
        }
        // 给长按手势定回调
        auto onActionCallBackPanLongPress = [](ArkUI_GestureEvent *event, void *extraParam) {
            
            showUITextCallback("触摸Column", "触发长按事件");
            
            ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event);
    
            float velocity = OH_ArkUI_PanGesture_GetVelocity(event);
            float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event);
            float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event);
            float offsetX = OH_ArkUI_PanGesture_GetOffsetX(event);
            float offsetY = OH_ArkUI_PanGesture_GetOffsetY(event);
            float scale = OH_ArkUI_PinchGesture_GetScale(event);
            float centerX = OH_ArkUI_PinchGesture_GetCenterX(event);
            float centerY = OH_ArkUI_PinchGesture_GetCenterY(event);
            float angle = OH_ArkUI_SwipeGesture_GetAngle(event);
            float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event);
            float angleR = OH_ArkUI_RotationGesture_GetAngle(event);
            float repeat = OH_ArkUI_LongPress_GetRepeatCount(event);
    
            OH_LOG_Print(
                LOG_APP, LOG_INFO, 0xFF00, "Manager",
                "GestureSampleLog,longPressGesturecallback actionType:%{public}d,velocity%{public}f,velocityX"
                "%{public}f;"
                "velocityY%{public}f,offsetX%{public}f,offsetY%{public}f,scale%{public}fcenterX"
                "%{public}fcenterY"
                "%{public}fangle%{public}fVelocityS%{public}fangleR%{public}frepeat%{public}f",
                actionType, velocity, velocityX, velocityY, offsetX, offsetY, scale, centerX, centerY, angle, VelocityS,
                angleR, repeat);
        };
        gestureApi->setGestureEventTarget(longPressGesture,
                                          GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | GESTURE_EVENT_ACTION_CANCEL,
                                          nodeHandle, onActionCallBackPanLongPress);
    
        // 将长按手势添加到手势组
        if (gestureApi->addChildGesture) {
            gestureApi->addChildGesture(groupGesture, longPressGesture);
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager", "GestureSampleLog, addChildGesture longPressGesture");
        }
        // 创建快滑手势 swipe
        auto swipeGesture = gestureApi->createSwipeGesture(1, GESTURE_DIRECTION_ALL, 100);
        if (gestureApi->getGestureType) {
            ArkUI_GestureRecognizerType type = gestureApi->getGestureType(swipeGesture);
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager",
                         "GestureSampleLog, ArkUI_GestureRecognizerType %{public}d", type);
        }
        // 给快滑手势绑定回调
        auto onActionCallBack = [](ArkUI_GestureEvent *event, void *extraParam) {
            
            showUITextCallback("触摸Column", "触发快滑事件");
            
            ArkUI_GestureEventActionType actionType = OH_ArkUI_GestureEvent_GetActionType(event);
    
            float velocity = OH_ArkUI_PanGesture_GetVelocity(event);
            float velocityX = OH_ArkUI_PanGesture_GetVelocityX(event);
            float velocityY = OH_ArkUI_PanGesture_GetVelocityY(event);
            float offsetX = OH_ArkUI_PanGesture_GetOffsetX(event);
            float offsetY = OH_ArkUI_PanGesture_GetOffsetY(event);
            float scale = OH_ArkUI_PinchGesture_GetScale(event);
            float centerX = OH_ArkUI_PinchGesture_GetCenterX(event);
            float centerY = OH_ArkUI_PinchGesture_GetCenterY(event);
            float angle = OH_ArkUI_SwipeGesture_GetAngle(event);
            float VelocityS = OH_ArkUI_SwipeGesture_GetVelocity(event);
            float angleR = OH_ArkUI_RotationGesture_GetAngle(event);
            float repeat = OH_ArkUI_LongPress_GetRepeatCount(event);
    
    
            // 通过日志查看
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager",
                         "GestureSampleLog, swipeGesture callback actionType: %{public}d, velocity "
                         "%{public}f,velocityX "
                         "%{public}f; "
                         "velocityY %{public}f, offsetX %{public}f, offsetY %{public}f, scale %{public}fcenterX "
                         "%{public}f centerY"
                         " %{public}f angle %{public}f VelocityS %{public}f angleR %{public}f repeat %{public}f",
                         actionType, velocity, velocityX, velocityY, offsetX, offsetY, scale, centerX, centerY, angle,
                         VelocityS, angleR, repeat);
    
            ArkUI_NumberValue value[] = {{.f32 = 0}, {.f32 = 0}, {.f32 = 0}, {.f32 = angleR}, {.f32 = 0}};
            ArkUI_AttributeItem item = {value, 5};
            auto column = reinterpret_cast<ArkUI_NodeHandle>(extraParam);
            auto nodeAPI = NativeModuleInstance::GetInstance()->GetNativeNodeAPI();
            nodeAPI->setAttribute(column, NODE_ROTATE, &item);
        };
    
        gestureApi->setGestureEventTarget(
            swipeGesture, GESTURE_EVENT_ACTION_ACCEPT | GESTURE_EVENT_ACTION_UPDATE | GESTURE_EVENT_ACTION_END, nodeHandle,
            onActionCallBack);
    
        // 将快滑手势添加到手势组
        if (gestureApi->addChildGesture) {
            gestureApi->addChildGesture(groupGesture, swipeGesture);
            OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager",
                         "GestureSampleLog, addChildGesture swipeGesture");
        }
        // 将手势组设置到组件上
        gestureApi->addGestureToNode(nodeHandle, groupGesture, PRIORITY, NORMAL_GESTURE_MASK);
        
        // 返回 Column 节点
        return column;
    } else {
        OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "Manager",
                     "GestureSampleLog, createGroupGesture api not exist");
        return column;
    }
}
}

#endif //NATIVEPC_TESTGESTUREEXAMPLE_H
